package com.example.a1.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.a1.R;
import com.example.a1.model.Category;
import java.util.List;

public class CategoryAdapter extends BaseAdapter {
    private Context context;
    private List<Category> categoryList;
    private LayoutInflater inflater;

    // 本地分类图标资源（编程语言、移动开发、前端开发、后端开发）
    private int[] localIcons = {
            R.drawable.ic_programming,    // 编程语言
            R.drawable.ic_mobile,         // 移动开发
            R.drawable.ic_frontend,       // 前端开发
            R.drawable.ic_backend         // 后端开发
    };

    public CategoryAdapter(Context context, List<Category> categoryList) {
        this.context = context;
        this.categoryList = categoryList;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return categoryList.size();
    }

    @Override
    public Object getItem(int position) {
        return categoryList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return categoryList.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_category, parent, false);
            holder = new ViewHolder();
            holder.icon = convertView.findViewById(R.id.category_icon);
            holder.name = convertView.findViewById(R.id.category_name);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Category category = categoryList.get(position);
        holder.name.setText(category.getName());

        ViewGroup.LayoutParams iconParams = holder.icon.getLayoutParams();
        iconParams.width = 60; // 图标宽度
        iconParams.height = 60; // 图标高度
        holder.icon.setLayoutParams(iconParams);

        // 设置本地图标
        if (position < localIcons.length) {
            holder.icon.setImageResource(localIcons[position]);
        } else {
            holder.icon.setImageResource(R.drawable.ic_dashboard_black_24dp);
        }

        return convertView;
    }

    static class ViewHolder {
        ImageView icon;
        TextView name;
    }
}